﻿/*-*-* Copyright (c) zouhangte@uframe
 * Author: zouhangte
 * Creation Date: 2024-08-20
 * Version: 1.0.0
 * Description: 
 *_*/

using System;
using UnityEngine;

namespace UFrame.StateTree
{
    public class StateTreeMechine 
    {
        private IState lastState;
        private IState currentState;

        public StateTreeMechine(IState initialState)
        {
            currentState = initialState;
            currentState.Enter();
        }

        public void Update()
        {
            currentState.Execute();
            IState nextState = currentState.CheckTransitions();
            if (nextState != null)
            {
                TransitionTo(nextState);
            }
        }

        private void TransitionTo(IState nextState)
        {
            lastState = currentState;
            currentState.Exit();
            currentState = nextState;
            currentState.Enter();
        }
    }
}

